package ch.sv7.tool.dbmanager.db.conversion;

import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.jmock.Mock;
import org.jmock.MockObjectTestCase;
/**
 * TODO: Document me!
 *
 * @author svt
 *
 */
public class UnicodeCustomTypeConversionTestCase extends MockObjectTestCase  {

	
	public void testConversionSimple() throws Exception {
		UnicodeCustomTypeConversion conv = new UnicodeCustomTypeConversion();
		Mock mockResultSet = mock(ResultSet.class);
		Mock mockPStmt = mock(PreparedStatement.class);
		
		String data = "test";
		byte[] bytes = data.getBytes("UTF8");
		
		mockResultSet.expects(atLeastOnce()).method("getBytes")
			.will( onConsecutiveCalls(
					returnValue(bytes),
					returnValue(bytes)));
		mockPStmt.expects(once()).method("setString").with(ANYTHING, eq(data));
		
		boolean update = conv.convertType((ResultSet)mockResultSet.proxy(), null, (PreparedStatement)mockPStmt.proxy(), 0, null, null);
		assertTrue("Result should be true",update);
	}
	
	public void testConversionNull() throws Exception {
		UnicodeCustomTypeConversion conv = new UnicodeCustomTypeConversion();
		Mock mockResultSet = mock(ResultSet.class);
		
		String data = "test";
		byte[] bytes = data.getBytes("UTF8");
		
		mockResultSet.expects(atLeastOnce()).method("getBytes")
			.will( onConsecutiveCalls(
					returnValue(null),
					returnValue(null)));
		boolean update = conv.convertType((ResultSet)mockResultSet.proxy(), null, null, 0, null, null);
		assertFalse("Result should be false", update);
	}

		
	public void testConversionUnicode() throws Exception {
		UnicodeCustomTypeConversion conv = new UnicodeCustomTypeConversion();
		Mock mockResultSet = mock(ResultSet.class);
		Mock mockPStmt = mock(PreparedStatement.class);
		
		String data = "\u00F6";
		byte[] bytes = data.getBytes("UTF8");
		
		mockResultSet.expects(atLeastOnce()).method("getBytes")
			.will( onConsecutiveCalls(
					returnValue(bytes),
					returnValue(bytes)));
		mockPStmt.expects(once()).method("setString").with(ANYTHING, eq(data));
		
		boolean update = conv.convertType((ResultSet)mockResultSet.proxy(), null, (PreparedStatement)mockPStmt.proxy(), 0, null, null);
		assertTrue("Result should be true",update);

	}
		
	public void testConversionError() throws Exception {
		UnicodeCustomTypeConversion conv = new UnicodeCustomTypeConversion();
		Mock mockResultSet = mock(ResultSet.class);
		
		String data = "test";
		byte[] bytes = data.getBytes("UTF8");
		conv.setCharsetName("BADCharset");
		assertNotNull(conv.getCharsetName());
		
		mockResultSet.expects(atLeastOnce()).method("getBytes")
			.will( onConsecutiveCalls(
					returnValue(null),
					returnValue(null)));
		boolean update = conv.convertType((ResultSet)mockResultSet.proxy(), null, null, 0, null, null);
		assertFalse("Result should be false", update);

	}
		
}
